<?php
//每日0：00定时运行 以清理当日答题获得但未使用的邀请码
function code_clear(){
    include 'bbs_invite_code_remote.php';
    //include 'tools/tools.php';
    $db = local_db();
    $sheet = 'invite_code';
    $date = date("Y-m-d",strtotime("-1 day"));
        $sql = "SELECT * FROM `$sheet` WHERE status='0' AND date='$date'";
        $result = $db->query($sql);
        $used = 0;
        $unused = 0;
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $code_info = getcode_by_code($row['code']);
                $index = $row['No'];
                if($code_info['status']=='0'){
                    delete_by_id($row['id']);
                    $sql = "DELETE FROM `invite_code` WHERE `invite_code`.`No` = $index";
                    $db->query($sql);
                    $unused++;
                }
                else if($code_info['status']=='1'){
                    $sql = "UPDATE `$sheet` SET `status` = 1 WHERE `invite_code`.`No` = $index";
                    $db->query($sql);
                    $used++;
                }
            }
        } else {
        }
        $total = $unused+$used;
        echo "finished\n";
        echo "total:".$total."\n";
        echo "used:".$used."\n";
        echo "unused:".$unused."\n";
}

code_clear();
